package speedball;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.Slider;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class TestProgressBar extends Application {
//	private static final String RED_BAR    = "red-bar";
//	  private static final String YELLOW_BAR = "yellow-bar";
//	  private static final String ORANGE_BAR = "orange-bar";
//	  private static final String GREEN_BAR  = "green-bar";
	  private static final String RED_BAR    = "-fx-accent:red;";
	  private static final String YELLOW_BAR = "-fx-accent:yellow;";
	  private static final String ORANGE_BAR = "-fx-accent:orange;";
	  private static final String GREEN_BAR  = "-fx-accent:green;";
	  
	  private static final String[] barColorStyleClasses = { RED_BAR, ORANGE_BAR, YELLOW_BAR, GREEN_BAR };
	@Override
	public void start(Stage stage) {
		Group root = new Group();
		Scene scene = new Scene(root);
		stage.setScene(scene);
		stage.setTitle("Progress Controls");

		final Slider slider = new Slider();
		slider.setMin(0);
		slider.setMax(50);

		final ProgressBar pb = new ProgressBar(0);
		pb.setRotate(-90);
		final ProgressIndicator pi = new ProgressIndicator(0);
//		pi.setRotate(90);

		slider.valueProperty().addListener(new ChangeListener<Number>() {
			public void changed(ObservableValue<? extends Number> ov, Number old_val, Number new_val) {
				pb.setProgress(new_val.doubleValue() / 50);
				pi.setProgress(new_val.doubleValue() / 50);
			}
		});
		
		pb.progressProperty().addListener(new ChangeListener<Number>() {
		      @Override 
		      public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
		        double progress = newValue == null ? 0 : newValue.doubleValue();
		        System.out.println(progress);
		        if (progress < 0.2) {
		          setBarStyleClass(pb, RED_BAR);
		        } else if (progress < 0.4) {
		          setBarStyleClass(pb, ORANGE_BAR);
		        } else if (progress < 0.6) {
		          setBarStyleClass(pb, YELLOW_BAR);
		        } else {
		          setBarStyleClass(pb, GREEN_BAR);
		        }
		      }
		      private void setBarStyleClass(ProgressBar bar, String barStyleClass) {
//		          bar.getStyleClass().removeAll(barColorStyleClasses);
//		          bar.getStyleClass().add(barStyleClass);
		          bar.setStyle(barStyleClass);
		        }
		});
		final HBox hb = new HBox();
		hb.setSpacing(5);
		hb.setAlignment(Pos.CENTER);
		hb.getChildren().addAll(slider, pb, pi);
		scene.setRoot(hb);
		stage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
